package com.sdp.wx.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_assets_add.entity.WxAssetsAdd;
import com.sdp.wx.db.wx_assets_add.service.IWxAssetsAddService;
import com.sdp.wx.db.wx_assets_info.entity.WxAssetsInfo;
import com.sdp.wx.db.wx_assets_info.service.IWxAssetsInfoService;
import com.sdp.wx.utils.CurrentLoginEmp;
import com.sdp.wx.utils.WxConstants;

import cn.hutool.core.bean.BeanUtil;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/WxAssetsAddController")
public class WxAssetsAddController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxAssetsAddService assetsAddService;
	
	@Autowired
	private IWxAssetsInfoService assetsInfoService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(assetsAddService, WxAssetsAdd.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxAssetsAdd assetsAdd) {
		if(assetsAdd == null){
			return Result.error("信息为空");
		}
		JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
		if (emp == null || !emp.containsKey("empNo")) {
			return Result.error("当前登录人信息为空");
		}
		if(StringUtils.isEmpty(assetsAdd.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(assetsAddService, WxAssetsAdd.class, new String[] {"assetNo"}, "keyId", assetsAdd, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("资产编号不能重复");
			}
			assetsAdd.setEmpNo(emp.getString("empNo"));
			assetsAdd.setEmpName(emp.getString("empName"));
			assetsAdd.setIsAssets(WxConstants.否.getCode());
			assetsAdd.setCreateTime(LocalDateTime.now());
			assetsAddService.save(assetsAdd);
		} else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(assetsAddService, WxAssetsAdd.class, new String[] {"assetNo"}, "keyId", null, assetsAdd, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("资产编号不能重复");
			}
			assetsAddService.updateById(assetsAdd);
		}
		
		return Result.ok(assetsAdd);
	}
	
	@GetMapping(value = "/del")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		assetsAddService.removeById(keyId);
		return Result.ok();
	}
	
	@GetMapping(value = "/sync")
	public Result<?> sync(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		QueryWrapper<WxAssetsAdd> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(WxAssetsAdd::getKeyId, keyId);
		WxAssetsAdd assetsAdd = assetsAddService.getOne(queryWrapper, false);
		if (assetsAdd == null) {
			return Result.error("需要同步的固定资产报告不存在");
		}
		if (!WxConstants.是.getCode().equals(assetsAdd.getIsAssets())) {
			WxAssetsInfo assetsInfo = BeanUtil.toBean(assetsAdd, WxAssetsInfo.class);
			assetsInfo.setUseStatus(WxConstants.否.getCode());
			assetsInfo.setScrapStatus(WxConstants.否.getCode());
			assetsInfo.setScrapHandleStatus(WxConstants.否.getCode());
			assetsInfoService.save(assetsInfo);
			
			assetsAdd.setIsAssets(WxConstants.是.getCode());
			assetsAddService.updateById(assetsAdd);
			return Result.ok();
		} else {
			return Result.error("固定资产报告已经同步过");
		}
		
	}
}

